package com.dou.cloud.bridge.redis.handler;


import com.dou.cloud.bridge.redis.enums.RedissonEnum;
import com.due.basic.tookit.function.DueVoidProducer;
import lombok.SneakyThrows;
import org.redisson.api.RPermitExpirableSemaphore;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * @Author daizl
 * @Date 2024 06 19 18 06
 **/
@Component
public class PermitExpirableSemaphore extends AbstractDistributedLockHandler{

    @SneakyThrows
    @Override
    public <T> T tryLock(Supplier<Object> supplier, String key, long waitTime, long leaseTime, TimeUnit timeUnit) {
        isRedissonClient();
        RPermitExpirableSemaphore semaphore = redissonClient.getPermitExpirableSemaphore(key);
        // 获取一个信号，有效期只有2秒钟。
        String permitId = semaphore.acquire(2, TimeUnit.SECONDS);
        semaphore.release(permitId);
        return null;
    }

    @Override
    public Integer getType() {
        return RedissonEnum.PERMIT_EXPIRABLE_SEMAPHORE.getType();
    }
}
